package com.fourth.service.impl;

import com.fourth.dao.EmpInfoDao;
import com.fourth.service.EmpInfoService;
import com.fourth.vo.EmpConditionVO;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.ljx.entity.EmpInfo;
import com.ljx.security.util.WebUtils;
import com.ljx.utils.JwtUtil;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * @author Mr.ma
 * @since 2023/2/27 13:56
 **/
@Service
public class EmpInfoServiceImpl implements EmpInfoService {
    @Resource
    private EmpInfoDao empInfoDao;

    /**
     * 获取当前登录用户的用户名
     * @return
     */
    private static String getUserName(){
        String token = WebUtils.getRequest().getHeader("token");
        Map<String, Object> tokenChaim = JwtUtil.getTokenChaim(token);
        return tokenChaim.get("username").toString();
    }
    @Override
    public PageInfo<EmpInfo> selectAll(Integer currentPage, Integer pageSize, EmpConditionVO condition) {
        PageHelper.startPage(currentPage,pageSize);
        List<EmpInfo> empInfos = empInfoDao.selectAll(condition);
        PageInfo<EmpInfo> empInfoPageInfo = new PageInfo<>(empInfos);
        return empInfoPageInfo;
    }

    @Override
    public int insert(EmpInfo empInfo) {
        empInfo.setGmtCreate(new Date());
        empInfo.setUpdatemanid(getUserName());
        empInfo.setIsDeleted(0);
        int insert = empInfoDao.insert(empInfo);
        return insert;
    }

    @Override
    public int updateEmp(EmpInfo empInfo) {
        empInfo.setGmtModified(new Date());
        empInfo.setUpdatemanid(getUserName());
        int update = empInfoDao.updateById(empInfo);
        return update;
    }

    @Override
    public int deleteById(String id) {
        int delete = empInfoDao.deleteById(id);
        return delete;
    }
}
